package map
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import utils.QhRectangle;
	import utils.QhUtils;
	
	/**
	 * 地图渲染类
	 * @author gcxk721
	 * 2012-6-13
	 * 下午4:50:42
	 */
	public class QhMap
	{
		
		/**
		 * 内部虚拟的地图 
		 */
		private var _virtualMap : Sprite;
		
		private var _container : Bitmap;
		
		/**
		 * 真正显示的地图 
		 */
		private var _map : BitmapData;
		
		/**
		 * 绘制地图时使用的点 
		 */
		private var _drawPoint : Point = new Point();
		
		/**
		 * 复制像素时，使用的点 
		 */
		private var _copyPoint : Point = new Point();
		
		/**
		 * 上次绘制的大小 
		 */
//		private var _passRec : Rectangle;
		private var _passRec : QhRectangle;
		
		/**
		 * 记录已经有的地图数据 
		 */
		private var _recordArray : Array;
		
		private var matrix : Matrix = new Matrix();
		
		public function QhMap()
		{
			_container = new Bitmap();
			_recordArray = new Array();
			_passRec = new QhRectangle();
			_virtualMap = new Sprite();
		}
		
		/**
		 * 添加分块的地图文件到地图中 
		 * @param x
		 * @param y
		 * @param bmd
		 * 
		 */		
		public function addMapImage(x : int, y : int, bmd : BitmapData, isPre : Boolean = false) : void
		{
			var bitmap : Bitmap = new Bitmap(bmd);
			bitmap.x = x;
			bitmap.y = y;
			_virtualMap.addChild(bitmap);
			
			if(isPre || checkDraw(x, y))
			{
				drawmap();
			}
		}
		
		public function addMaskLayer(bmp : Bitmap) : void
		{
			_virtualMap.addChildAt(bmp, 0);
		}
		
		protected function checkDraw(x : int, y : int) : Boolean
		{
			return _passRec.contains(x, y);
		}
		
		/**
		 * 绘制地图 
		 * @param rec
		 * 
		 */		
		public function draw(rec : QhRectangle) : void
		{
			if(isResize ||_passRec.x != rec.x || _passRec.y != rec.y || _passRec.width != rec.width || _passRec.height != rec.height)
			{
				isResize = true;
				_passRec.x = rec.x;
				_passRec.y = rec.y;
//				_passRec.width = rec.width;
//				_passRec.height = rec.height;
				drawmap();
			}
		}
		
		protected function drawmap() : void
		{
			matrix.tx = (-_passRec.x);
			matrix.ty = (-_passRec.y);
			_map.draw(_virtualMap, matrix); 
		}
		
		/**
		 * 当舞台大小发生变化
		 * @param w
		 * @param h
		 * 
		 */		
		private var isResize : Boolean = false;
		public function onResizeHandler(w : int, h : int) : void
		{
			if(_map != null)
			{
				_map.dispose();
			}
			w = w <= 0 ? 1 : w;
			h = h <= 0 ? 1 : h;
			
			_map = new BitmapData(w, h, false, 0);
			_container.bitmapData = _map;
			isResize = true;
		}

		public function record(id : String) : void
		{
			_recordArray.push(id);
		}
		
		public function contains(id : String) : Boolean
		{
			return _recordArray.indexOf(id) == -1;
		}
		
		public function resetRecord() : void
		{
			_recordArray = new Array();
			QhUtils.deleteContainer(_virtualMap);
		}
		
		/**
		 * 地图事件容器 
		 */
		public function get container():Bitmap
		{
			return _container;
		}
		
	}
}